package com.sanxiau.dormitory.liu.controller;


import com.github.pagehelper.PageInfo;
import com.sanxiau.dormitory.liu.dao.*;
import com.sanxiau.dormitory.liu.entity.Dorsanit;
import com.sanxiau.dormitory.liu.service.DorsanitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 寝室卫生控制
 */
@Controller
@RequestMapping("/dorsanit")
public class DorsanitController {

    @Autowired
    NoticeDao noticeDao;

    @Autowired
    DorsanitService dorsanitService;

    @Autowired
    DorsanitDao dorsanitDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    HouseDao houseDao;

    @Autowired
    UserDao userDao;


    //学生看寝室卫生
    @RequestMapping("/goDorsanitToStudent")
    public String goDorsanitToStudent(Model model, HttpSession session){

        try {
            //寝室楼编号
            List<Dorsanit> dorsanits = new ArrayList<>();
            //得到登录账户
            String userName = session.getAttribute("userName").toString();
            //根据学号查询寝室楼号
            String houId = studentDao.findByStuId(userName).get(0).getHouId();
            dorsanits = dorsanitDao.findDorsanitByHouId(houId);
            model.addAttribute("dorsanits",dorsanits.get(0));
        }catch (Exception e) {
        return "liu/error";
        }
        return "liu/dorsanitToStudent";
    }

    //去卫生页面
    @RequestMapping("/goDorsanitByHouId")
    public String goDorsanitByHouId() {
        return "liu/dorsanitByHouId";
    }


    //去增加卫生页面
    @RequestMapping("/goAddDorsanit")
    public String goAddDorsanit() {
        return "liu/addDorsanit";
    }


    //查询以前卫生
    @ResponseBody
    @PostMapping("/DorsanitByHouId")
    public PageInfo<Dorsanit> DorsanitByHouId(int pageNum, int pageSize, HttpSession session) {
        if(pageNum==-1){
            pageNum=1;
        }
        String houId = null;
        try {
            //得到登录账户
            String userName = session.getAttribute("userName").toString();
            //根据宿管查询寝室楼号
            houId = houseDao.findByUserName(userName).get(0).getHouId();
        } catch (Exception e) {
            houId = "error";
            return dorsanitService.findByHouId(pageNum,pageSize,houId);
        }
        //分页
        return dorsanitService.findByHouId(pageNum,pageSize,houId);
    }


    //根据id查询卫生
    @ResponseBody
    @GetMapping("/findOneDorsanit")
    public List<Dorsanit> findOneDorsanit(int id) {
        System.out.println("....." + id);
        List<Dorsanit> dorsanits = new ArrayList<>();
        dorsanits = dorsanitDao.findById1(id);
        return dorsanits;
    }



    //增加卫生
    @PostMapping("/addDorsanit")
    public String addDorsanit(Dorsanit dorsanit, HttpSession session) {
        try {

            //得到当前时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String date = simpleDateFormat.format(new Date());
            dorsanit.setSanitDate(date);
            //得到登录账户
            String userName = session.getAttribute("userName").toString();
            //根据账号查询寝室楼号
            String houId = houseDao.findByUserName(userName).get(0).getHouId();
            dorsanit.setHouId(houId);
            //值班人
            String watch = userDao.findUserByUserName(userName).get(0).getTrueName();
            dorsanit.setWatch(watch);

            dorsanitDao.save(dorsanit);
            return "liu/success";
        } catch (Exception e) {
            return "liu/error";
        }

    }

}
